home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / Managed / DirectSound / EnumDevices / wfEnum.cs < prev    next >
Encoding:
Text File  |  2004-09-27  |  7.9 KB  |  249 lines

  1. //----------------------------------------------------------------------------
  2. // File: wfEnum.cs
  3. //
  4. // Copyright (c) Microsoft Corp. All rights reserved.
  5. //-----------------------------------------------------------------------------
  6. using System;
  7. using System.Drawing;
  8. using System.Collections;
  9. using System.ComponentModel;
  10. using System.Windows.Forms;
  11. using Microsoft.DirectX.DirectSound;
  12.  
  13. namespace csEnumDevices
  14. {
  15.     public class wfEnum : System.Windows.Forms.Form
  16.     {
  17.         private struct DeviceDescription
  18.         {
  19.             public DeviceInformation info;
  20.             public override string ToString()
  21.             {
  22.                 return info.Description;
  23.             }
  24.             public DeviceDescription(DeviceInformation d)
  25.             {
  26.                 info = d;
  27.             }
  28.         }
  29.         // The devices
  30.         private Device applicationDevice = null;
  31.         private Capture applicationCapture = null;
  32.         private DevicesCollection devicesCollection = null;
  33.         private CaptureDevicesCollection captureCollection = null;
  34.         private System.Windows.Forms.Label label1;
  35.         private System.Windows.Forms.Label label2;
  36.         private System.Windows.Forms.Label label3;
  37.         private System.Windows.Forms.ComboBox comboboxSound;
  38.         private System.Windows.Forms.ComboBox comboboxCapture;
  39.         private System.Windows.Forms.Button buttonCreate;
  40.         private System.Windows.Forms.Button buttonExit;
  41.         /// <summary>
  42.         /// Required designer variable.
  43.         /// </summary>
  44.         private System.ComponentModel.Container components = null;
  45.         public wfEnum()
  46.         {
  47.             //
  48.             // Required for Windows Form Designer support
  49.             //
  50.             InitializeComponent();
  51.  
  52.             // Retrieve the DirectSound Devices first.
  53.             devicesCollection = new DevicesCollection();
  54.             
  55.             foreach (DeviceInformation dev in devicesCollection)
  56.             {
  57.                 DeviceDescription dd = new DeviceDescription(dev);
  58.                 comboboxSound.Items.Add(dd);
  59.             }
  60.  
  61.             // Select the first item in the combobox
  62.             if (0 < comboboxSound.Items.Count)
  63.                 comboboxSound.SelectedIndex = 0;
  64.             
  65.             // Now the capture devices
  66.             captureCollection = new CaptureDevicesCollection();
  67.             
  68.             foreach (DeviceInformation dev in captureCollection)
  69.             {
  70.                 DeviceDescription dd = new DeviceDescription(dev);
  71.                 comboboxCapture.Items.Add(dd);
  72.             }
  73.  
  74.             // Select the first item in the combobox
  75.             if (comboboxCapture.Items.Count > 0)
  76.                 comboboxCapture.SelectedIndex = 0;
  77.         }
  78.  
  79.         /// <summary>
  80.         /// Clean up any resources being used.
  81.         /// </summary>
  82.         protected override void Dispose(bool disposing)
  83.         {
  84.             if (disposing)
  85.             {
  86.                 if (components != null) 
  87.                 {
  88.                     components.Dispose();
  89.                 }
  90.             }
  91.             base.Dispose(disposing);
  92.         }
  93.  
  94.         #region Windows Form Designer generated code
  95.         /// <summary>
  96.         /// Required method for Designer support - do not modify
  97.         /// the contents of this method with the code editor.
  98.         /// </summary>
  99.         private void InitializeComponent()
  100.         {
  101.             this.label1 = new System.Windows.Forms.Label();
  102.             this.label2 = new System.Windows.Forms.Label();
  103.             this.label3 = new System.Windows.Forms.Label();
  104.             this.comboboxCapture = new System.Windows.Forms.ComboBox();
  105.             this.buttonCreate = new System.Windows.Forms.Button();
  106.             this.buttonExit = new System.Windows.Forms.Button();
  107.             this.comboboxSound = new System.Windows.Forms.ComboBox();
  108.             this.SuspendLayout();
  109.             // 
  110.             // label1
  111.             // 
  112.             this.label1.Location = new System.Drawing.Point(6, 4);
  113.             this.label1.Name = "label1";
  114.             this.label1.Size = new System.Drawing.Size(288, 15);
  115.             this.label1.TabIndex = 0;
  116.             this.label1.Text = "This sample simply shows how to enumerate devices.";
  117.             // 
  118.             // label2
  119.             // 
  120.             this.label2.Location = new System.Drawing.Point(6, 29);
  121.             this.label2.Name = "label2";
  122.             this.label2.Size = new System.Drawing.Size(79, 17);
  123.             this.label2.TabIndex = 1;
  124.             this.label2.Text = "Sound Device:";
  125.             // 
  126.             // label3
  127.             // 
  128.             this.label3.Location = new System.Drawing.Point(6, 53);
  129.             this.label3.Name = "label3";
  130.             this.label3.Size = new System.Drawing.Size(91, 17);
  131.             this.label3.TabIndex = 1;
  132.             this.label3.Text = "Capture Device:";
  133.             // 
  134.             // comboboxCapture
  135.             // 
  136.             this.comboboxCapture.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  137.             this.comboboxCapture.DropDownWidth = 202;
  138.             this.comboboxCapture.Location = new System.Drawing.Point(92, 51);
  139.             this.comboboxCapture.Name = "comboboxCapture";
  140.             this.comboboxCapture.Size = new System.Drawing.Size(202, 21);
  141.             this.comboboxCapture.TabIndex = 3;
  142.             // 
  143.             // buttonCreate
  144.             // 
  145.             this.buttonCreate.Location = new System.Drawing.Point(9, 81);
  146.             this.buttonCreate.Name = "buttonCreate";
  147.             this.buttonCreate.Size = new System.Drawing.Size(86, 21);
  148.             this.buttonCreate.TabIndex = 4;
  149.             this.buttonCreate.Text = "Create";
  150.             this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click);
  151.             // 
  152.             // buttonExit
  153.             // 
  154.             this.buttonExit.DialogResult = System.Windows.Forms.DialogResult.Cancel;
  155.             this.buttonExit.Location = new System.Drawing.Point(205, 81);
  156.             this.buttonExit.Name = "buttonExit";
  157.             this.buttonExit.Size = new System.Drawing.Size(86, 21);
  158.             this.buttonExit.TabIndex = 4;
  159.             this.buttonExit.Text = "Exit";
  160.             this.buttonExit.Click += new System.EventHandler(this.buttonExit_Click);
  161.             // 
  162.             // comboboxSound
  163.             // 
  164.             this.comboboxSound.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  165.             this.comboboxSound.DropDownWidth = 202;
  166.             this.comboboxSound.Location = new System.Drawing.Point(92, 27);
  167.             this.comboboxSound.Name = "comboboxSound";
  168.             this.comboboxSound.Size = new System.Drawing.Size(202, 21);
  169.             this.comboboxSound.TabIndex = 2;
  170.             // 
  171.             // wfEnum
  172.             // 
  173.             this.AcceptButton = this.buttonCreate;
  174.             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  175.             this.CancelButton = this.buttonExit;
  176.             this.ClientSize = new System.Drawing.Size(300, 121);
  177.             this.Controls.AddRange(new System.Windows.Forms.Control[] {
  178.                                                                           this.buttonExit,
  179.                                                                           this.buttonCreate,
  180.                                                                           this.comboboxCapture,
  181.                                                                           this.comboboxSound,
  182.                                                                           this.label3,
  183.                                                                           this.label2,
  184.                                                                           this.label1});
  185.             this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
  186.             this.Name = "wfEnum";
  187.             this.Text = "DirectSound Enumerate Devices";
  188.             this.ResumeLayout(false);
  189.  
  190.         }
  191.         #endregion
  192.         /// <summary>
  193.         /// The main entry point for the application.
  194.         /// </summary>
  195.         static void Main() 
  196.         {
  197.             Application.Run(new wfEnum());
  198.         }
  199.  
  200.         private void buttonExit_Click(object sender, System.EventArgs e)
  201.         {
  202.             this.Dispose();
  203.         }
  204.  
  205.         private void buttonCreate_Click(object sender, System.EventArgs e)
  206.         {
  207.             DeviceDescription itemSelect;
  208.  
  209.             // Check to see if there are any devices available.
  210.             if ((0 == comboboxSound.Items.Count) && (0 == comboboxCapture.Items.Count))
  211.             {
  212.                 MessageBox.Show("No devices available.");
  213.                 return;
  214.             }
  215.  
  216.             // First try to create the Sound Device
  217.             try
  218.             {
  219.                 itemSelect = (DeviceDescription)comboboxSound.Items[comboboxSound.SelectedIndex];
  220.                 if (Guid.Empty == itemSelect.info.DriverGuid)
  221.                     applicationDevice = new Device();
  222.                 else
  223.                     applicationDevice = new Device(itemSelect.info.DriverGuid);
  224.             }
  225.             catch
  226.             {
  227.                 MessageBox.Show("Could not create DirectSound device.", "Failure!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  228.                 return;
  229.             }
  230.             
  231.             // Next try to create a capture device
  232.             try
  233.             {
  234.                 itemSelect = (DeviceDescription)comboboxCapture.Items[comboboxCapture.SelectedIndex];
  235.                 if (Guid.Empty == itemSelect.info.DriverGuid)
  236.                     applicationCapture = new Capture();
  237.                 else
  238.                     applicationCapture = new Capture(itemSelect.info.DriverGuid);
  239.             }
  240.             catch
  241.             {
  242.                 MessageBox.Show("Could not create DirectSound Capture device.", "Failure!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  243.                 return;
  244.             }
  245.             MessageBox.Show("Devices created successfully.", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information);
  246.         }
  247.     }
  248. }
  249.